//
//  PainterLineAVG.m
//  p4
//
//  Created by CaoJun on 15/11/27.
//  Copyright © 2015年 com.baidao. All rights reserved.
//

#import "PainterLineAVG.h"
#import "AVGData.h"

using namespace std;
using namespace ytx;

@implementation PainterLineAVG

- (void)drawRect:(CGRect)cgrect withContext:(ChartCanvas *) ctx withViewModelIndexPosition:(ViewModelIndexPosition *) indexPosition
{
    [super drawRect:cgrect withContext:ctx withViewModelIndexPosition:indexPosition];
    AVGData * data = (AVGData *)indexPosition->getData();
    vector<double> lineData = data->lineData[0]; //值线
    YTXConfig * config = YTXConfig::getInstance();
    ytx::Style style = config->style;
    Rectangle & rect = indexPosition->getRectangle();
    
    if (style.isAvgValueShadowColor) {
        NSString * avgValueShadowColor = [ NSString stringWithUTF8String: style.avgValueShadowColor.c_str() ];
        float beginX = rect.getBeginX();
        if(data->lineDataDrawPartIndex.size()){
            const std::vector<pair<int,int> > & partIndex = data->lineDataDrawPartIndex;
            
            for (std::vector<pair<int,int> >::const_iterator it = partIndex.begin(); it != partIndex.end(); ++it) {
                pair<int,int> index = (*it);
                //            beginX = drawLine(beginX, lineData.begin()+index.first, lineData.begin()+ index.second);
                beginX = [self drawShadow:cgrect withContext:ctx withViewModelIndexPosition: indexPosition withShadowColor:avgValueShadowColor withBeginX:beginX withIteratiorFirst:lineData.begin()+index.first withIteratiorLast:lineData.begin()+ index.second];
            }
            
        }
        else{
            int startIndex = indexPosition->getStartDataIndex();
            int endIndex = indexPosition->getEndDataIndex();
            //        drawLine(beginX, lineData.begin()+startIndex, lineData.begin()+ endIndex);
            [self drawShadow:cgrect withContext:ctx withViewModelIndexPosition: indexPosition withShadowColor:avgValueShadowColor withBeginX:beginX withIteratiorFirst:lineData.begin()+startIndex withIteratiorLast:lineData.begin()+ endIndex];
        }
    }
    
}

- (CGFloat)drawShadow:(CGRect)cgrect withContext:(ChartCanvas *) ctx withViewModelIndexPosition:(ViewModelIndexPosition *) indexPosition withShadowColor:(NSString *) color withBeginX:(CGFloat) beginX withIteratiorFirst:(vector<double>::const_iterator) first withIteratiorLast:(vector<double>::const_iterator) last
{
    Rectangle & rect = indexPosition->getRectangle();
    
    float zoomX = rect.getZoomX();
    float tempX = beginX;
    int tempY = 0;
    double value;
    NSMutableArray * points = [NSMutableArray new];
    [points addObject:[NSValue valueWithCGPoint:CGPointMake(tempX, rect.getEndY())]];
    for (vector<double>::const_iterator it = first; it != last; ++it) {
        value = (*it);
        tempX += zoomX;
        if(!isnan(value)){
            tempY = rect.getPxY(value);
            [points addObject: [NSValue valueWithCGPoint:CGPointMake(tempX, tempY)]];
            //            points.push_back(ccp(tempX,tempY));
        }
    }
    
    if(points.count == 0){
        return beginX;
    }
    
    if (points.count > 0 && [points[0] CGPointValue].x != beginX) {
        //        points.insert(points.begin(), ccp(beginX, points[0].y));
        [points insertObject:[NSValue valueWithCGPoint:CGPointMake(beginX, [points[0] CGPointValue].y)] atIndex:0];
    }
    
    [points addObject:[NSValue valueWithCGPoint:CGPointMake([points.lastObject CGPointValue].x, rect.getEndY())]];

    [ctx fillPath:points withFillColor:color];
    
    return tempX;
}

@end
